const path = require("path");
// const { uploadError } = require("../constant/err.type");
const { USER_SECRET } = require("../config/config.default");
const jwt = require("jsonwebtoken");
const {
  createGoods,
  getGoodsList,
  updateGoods,
  checkGoods,
  removeGoods,
  getGoodsType,
} = require("../service/goods.service");
class GoodsController {
  // 添加商品
  async addGoods(ctx, next) {
    const { goodsName, goodsPrice, goodsNum, category, goodsDesc, imgUrl } =
      ctx.request.body;
    const res = await createGoods({
      goodsName,
      goodsPrice,
      goodsNum,
      category,
      goodsDesc,
      imgUrl,
    });
    // console.log("商品添加运行结果", res);
    ctx.response.body = {
      code: "10009",
      message: "商品添加成功",
      data: {},
    };
    await next();
  }
  async getGoodsList(ctx, next) {
    // console.log("token", ctx.request.header.authorization);
    // const token = ctx.request.header.authorization;
    // if (token) {
    //   const token = ctx.request.header.authorization.split(" ").pop();
    //   console.log("token", token);
    //   const { role_type } = jwt.verify(token, USER_SECRET);
    //   console.log("role_type", role_type);
    //   if (role_type === "0") {
    //     // 超管权限展示全部商品
    //     // 获取pageNum和pageSize
    //     const { pageNum = 1, pageSize = 5, goodsName = "" } = ctx.query;
    //     // const { type, id } = ctx.query;
    //     const res = await getGoodsList(pageNum, pageSize, goodsName);
    //     ctx.response.body = {
    //       code: "10010",
    //       message: "获取商品列表成功",
    //       data: res,
    //     };
    //     await next();
    //   } else {
    //     // 商家权限展示自己上架的商品
    //     // 获取pageNum和pageSize
    //     const { pageNum = 1, pageSize = 5, goodsName = "" } = ctx.query;
    //     // const { type, id } = ctx.query;
    //     const res = await getGoodsList(pageNum, pageSize, goodsName);
    //     ctx.response.body = {
    //       code: "10010",
    //       message: "获取商品列表成功",
    //       data: res,
    //     };
    //     await next();
    //   }
    // }
    // 获取pageNum和pageSize
    const { pageNum = 1, pageSize = 5, goodsName = "" } = ctx.query;
    // const { type, id } = ctx.query;
    const res = await getGoodsList(pageNum, pageSize, goodsName);
    ctx.response.body = {
      code: "10010",
      message: "获取商品列表成功",
      data: res,
    };
    await next();
  }
  // 更新商品
  async updateGoods(ctx, next) {
    const { id, goodsName, goodsPrice, goodsNum, category, goodsDesc, imgUrl } =
      ctx.request.body;
    // console.log("商品更新", ctx.request.body);
    try {
      const res = await updateGoods(
        id,
        goodsName,
        goodsPrice,
        goodsNum,
        category,
        goodsDesc,
        imgUrl
      );
      if (res) {
        ctx.response.body = {
          code: "10011",
          message: "商品更新成功",
          data: {},
        };
      }
    } catch (error) {
      ctx.response.body = {
        code: "10012",
        message: "商品更新失败",
        data: {},
      };
    }

    await next();
  }

  async checkGoods(ctx, next) {
    const { id, state } = ctx.request.body;
    console.log("商品审核", ctx.request.body);
    try {
      const res = await checkGoods(id, state);
      if (res) {
        ctx.response.body = {
          code: "10013",
          message: "商品审核成功",
          data: {},
        };
      }
    } catch (error) {}

    await next();
  }

  async removeGoods(ctx, next) {
    const { id } = ctx.request.body;
    console.log("商品删除", ctx.request.body);
    try {
      const res = await removeGoods(id);
      if (res) {
        ctx.response.body = {
          code: "10014",
          message: "商品删除成功",
          data: {},
        };
      }
    } catch (error) {}

    await next();
  }

  // 获取商品分类
  async getGoodsCategory(ctx, next) {
    const {
      pageNum = 1,
      pageSize = 10,
      goodsName = "",
      category = "",
    } = ctx.query;
    const data = await getGoodsType(pageNum, pageSize, goodsName, category);
    ctx.response.body = {
      code: "10015",
      message: "获取商品分类成功",
      data,
    };
    await next();
  }
}

module.exports = new GoodsController();
